home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games Extra 1996 September / Amiga Games Extra CD-ROM 9-1996.iso / spiele / publicdomain / elan / src / ncurses / console.c next >
C/C++ Source or Header  |  1996-06-07  |  4KB  |  199 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5.  
  6. #include "global.h"
  7. #include "system.h"
  8. #include "console.h"
  9.  
  10.  
  11. int pen_colors[PEN_NUMBER];
  12. typedef struct {
  13.   int idx;
  14.   colors_pair pair;
  15. } colors_triple;
  16. colors_triple triples[PEN_NUMBER];
  17. int n_triples;
  18.  
  19. static int cmp_triple(const void *a, const void *b)
  20. {
  21.   colors_triple *a1 = (colors_triple *) a;
  22.   colors_triple *b1 = (colors_triple *) b;
  23.  
  24.   if (a1->pair.fg > b1->pair.fg)
  25.     return 1;
  26.   else if (a1->pair.fg < b1->pair.fg)
  27.     return -1;
  28.   else if (a1->pair.bg > b1->pair.bg)
  29.     return 1;
  30.   else if (a1->pair.bg < b1->pair.bg)
  31.     return -1;
  32.   else
  33.     return 0;
  34.   /*return ((a1->pair.fg - b1->pair.fg) ? 
  35.       (a1->pair.fg - b1->pair.fg) : (a1->pair.bg - b1->pair.bg));*/
  36. }
  37.  
  38. int
  39. open_console(const char *title_string, int x, int y, int l, int h)
  40. {
  41.   int i, n;
  42.  
  43.   initscr();
  44.   start_color();
  45.   cbreak();
  46.   noecho();
  47.   nonl();
  48.   intrflush(stdscr, FALSE);
  49.   /*keypad(stdscr, TRUE);*/
  50.  
  51.   if (!has_colors()) {
  52.     close_console();
  53.     fprintf(stderr,
  54.         "Elan needs a color terminal. Sorry.\n");
  55.     exit(1);
  56.   }
  57.   if (LINES < 45 || COLS < 80) { /* CHANGE: le dim. dovrebbero essere un po' meno hardwired! */
  58.     close_console();
  59.     fprintf(stderr, 
  60.         "Elan needs a terminal 80 chars wide * 45 lines high at least, sorry.\n");
  61.     exit(1);
  62.   }
  63.  
  64.   /* Now we minimize the # of color pairs */
  65.   qsort(triples, n_triples * 2, sizeof (colors_triple), cmp_triple);
  66.   n = 1;
  67.   init_pair(n, triples[0].pair.fg, triples[0].pair.bg);
  68.   pen_colors[triples[0].idx] = 1;
  69.   for (i = 1; i < n_triples * 2; i++) {
  70.     if (triples[i - 1].pair.fg == triples[i].pair.fg
  71.     && triples[i - 1].pair.bg == triples[i].pair.bg) {
  72.       pen_colors[triples[i].idx] = pen_colors[triples[i - 1].idx];
  73.       continue;
  74.     }
  75.     if (++n >= COLOR_PAIRS) {
  76.       close_console();
  77.       fprintf(stderr,
  78.           "This terminal supports too few colors.\n"
  79.           "Maybe you can change Elan configuration file to reduce the number of colors?\n");
  80.       exit(1);
  81.     }
  82.     init_pair(n, triples[i].pair.fg, triples[i].pair.bg);
  83.     pen_colors[triples[i].idx] = n;
  84.   }
  85.  
  86.   return 0;
  87. }
  88.  
  89. void
  90. close_console()
  91. {
  92.   free(triples);
  93.   endwin();
  94. }
  95.  
  96. char
  97. read_char()
  98. {
  99.   refresh();
  100.   nodelay(stdscr, FALSE);
  101.   return (getch());
  102. }
  103.  
  104. char
  105. async_read_char()
  106. {
  107.   char c;
  108.  
  109.   refresh();
  110.   nodelay(stdscr, TRUE);
  111.   c = getch();
  112.   return ((c == ERR) ? 0 : c);
  113. }
  114.  
  115. void
  116. con_printf(const char *format_string,...)
  117. {
  118.   char buffer[STRING_MAX_LEN];
  119.   va_list arg_list;
  120.  
  121.   va_start(arg_list, format_string);
  122.   vsprintf(buffer, format_string, arg_list);
  123.   write_string(buffer);
  124. }
  125.  
  126. int register_pen(colors_pair pair)
  127. {
  128.   if (n_triples >= PEN_NUMBER) {
  129.     fprintf(stderr, "INTERNAL ERROR: trying to register too many colors!\n");
  130.     exit(1);
  131.   }
  132.   triples[n_triples * 2].idx = n_triples;
  133.   triples[n_triples * 2].pair = pair;
  134.   triples[n_triples * 2 + 1].idx = reverse_pen(n_triples);
  135.   triples[n_triples * 2 + 1].pair.fg = pair.bg;
  136.   triples[n_triples * 2 + 1].pair.bg = pair.fg;
  137.  
  138.   return n_triples++;
  139. }
  140.  
  141. void set_pen(int pen)
  142. {
  143.   attron(COLOR_PAIR(pen_colors[pen]));
  144. }
  145.  
  146.  
  147. #ifdef DEBUG
  148. void
  149. locate(int i, int j)
  150. {
  151.   move(i, j);
  152. }
  153.  
  154. void
  155. clear_screen()
  156. {
  157.   erase();
  158. }
  159.  
  160. void
  161. clear_to_eos()
  162. {
  163.   clrtobot();
  164. }
  165.  
  166. void
  167. clear_to_eol()
  168. {
  169.   clrtoeol();
  170. }
  171.  
  172. void
  173. begin_redraw()
  174. {
  175.   clear();
  176. }
  177.  
  178. void
  179. end_redraw()
  180. {
  181.   touchwin(stdscr);
  182.   refresh();
  183.   redrawwin(stdscr);
  184. }
  185.  
  186. void
  187. write_char(char c)
  188. {
  189.   addch(c);            /* Outputs control characters other than tab,
  190.                    nl or bs as ^X ! */
  191. }
  192.  
  193. void
  194. write_string(const char *string)
  195. {
  196.   addstr(string);
  197. }
  198. #endif
  199.